﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Schema;
using Microsoft.ResourceManagement.Client;
using Microsoft.ResourceManagement.ObjectModel;
using Microsoft.ResourceManagement.ObjectModel.ResourceTypes;

namespace Quest.FIMPowerShellSnapin
{
    /// <summary>
    /// The SchemaCache class extends RmFactory by adding additional schema query functions
    /// </summary>
    public class SchemaCache : RmFactory
    {
        /// <summary>
        /// AttributeType expresses the types of attributes stored in the schema cache. Note that this enum is identical to RmAttributeType, which is
        /// protected.
        /// </summary>
        public enum AttributeType
        {
            String,
            Reference,
            DateTime,
            Integer,
            Binary,
            Boolean
        }

        #region Constructors
        /// <summary>
        /// Default ctor produces an empty cache.
        /// </summary>
        public SchemaCache()
            : base(new XmlSchemaSet())
        {
        }

        /// <summary>
        /// Ctor that produces a schema cache initialized with the specified XmlSchemaSet
        /// </summary>
        /// <param name="schemaSet">An XmlSchemaSet object initialized from the FIM service via the MEX endpoint.</param>
        public SchemaCache(XmlSchemaSet schemaSet)
            : base(schemaSet)
        {
        }
      
        #endregion
        #region Schema query functions
        public List<string> AllAttributesNames(String objectType)
        {
            List<string> retList = new List<string>();

            Dictionary<RmAttributeName, RmAttributeInfo> attributeValue = null;

            if (RmObjectCache.TryGetValue(objectType, out attributeValue))
            {
                foreach (KeyValuePair<RmAttributeName, RmAttributeInfo> pair in attributeValue)
                {
                    retList.Add(pair.Key.Name);
                }
            }
            return retList;
        }

        public AttributeType GetAttributeType(string attributeName)
        {
            return (AttributeType)(RmAttributeCache[new RmAttributeName(attributeName)]).AttributeType;
        }

        public bool IsMultiValued(string attributeName)
        {
            return base.IsMultiValued(new RmAttributeName(attributeName));
        }
        #endregion
    }
}

